<template>
    <div class="program-resources">
        <div class="sidebar">
            <div class="nav-list">
                <div class="head-container">
                    <el-input
                        v-model="nodeName"
                        placeholder="请输入节点名称"
                        clearable
                        size="small"
                        prefix-icon="el-icon-search"
                        style="margin-bottom: 20px"
                    />
                </div>
                <el-tree 
                    :data="chapterOptions"
                    :props="{ children: 'children', label: 'label' }"
                    :expand-on-click-node="false"
                    ref="tree"
                    node-key="id"
                    highlight-current
                    :default-expanded-keys="defaultShowNodes"
                    :indent="14"
                    @node-click="handleNodeClick"
                    class="custom-tree">
                    <span class="custom-tree-node" slot-scope="{ node }">
                        <span>{{ node.label }}</span>
                    </span>
                </el-tree>
            </div>
        </div>
        <div class="content">
            <div class="content_top_title">
                <i class="el-icon-s-opportunity"></i>
                实验视频：
            </div>
            <div v-if="!currentNode">
                <el-empty description="点击左侧列表获取数据" />
            </div>
            <div v-else>
                <div v-if="videoList.length === 0">
                    <el-empty description="暂无实验视频" />
                </div>
                <div v-else class="content_video_box">
                <div v-for="(video, index) in videoList" 
                     :key="index" 
                     class="content_video_box_item">
                    <div class="video-thumbnail">
                        <video 
                                :src="baseURL + video.crFileUrl" 
                            muted 
                            loop 
                            playsinline
                                class="video-player no-controls"
                            ></video>
                        <div class="play-button">
                            <i class="el-icon-video-play"></i>
                        </div>
                    </div>
                    <div class="video-info">
                        <div class="video-title" 
                                 :title="removeFileSuffix(video.crName)"
                                 ref="title">{{ removeFileSuffix(video.crName) }}</div>
                    </div>
                    <div class="buttom_box">
                        <div class="Btn bgcolor1"  @click="playVideo(video)">
                            <div class="sign">
                                <i class="el-icon-video-play"></i>
                            </div>
                            <div class="text">
                                播放
                            </div>
                        </div>
                            <div class="Btn bgcolor2" @click="downloadFile(video)">
                            <div class="sign">
                                <i class="el-icon-download"></i>
                            </div>
                            <div class="text">
                                下载
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
              <!-- 视频播放弹窗 -->
      <el-dialog
        :visible.sync="dialogVisible"
        width="70%"
        :before-close="handleClose"
        custom-class="video-dialog"
        :modal="true"
        :append-to-body="true"
        :modal-append-to-body="false"
      >
        <video
          v-if="currentVideo"
          ref="videoPlayer"
          :src="currentVideo.videoUrl"
          class="video-player"
          controls
          autoplay
        ></video>
      </el-dialog>
        </div>
    </div>
</template>

<script>
import courseRelated from '@/api/courseRelated'
export default {
    name: 'ProgramResources',
    data() {
        return {
            nodeName: '',
            chapterOptions: [], // 章节树
            defaultShowNodes: [],
            currentNode: null, // 当前选中节点
            videoList: [], // 当前节点下的实验视频
            dialogVisible: false,
            currentVideo: null,
            baseURL: process.env.VUE_APP_BASE_API,
        }
    },
    created() {
        this.getChapterTree()
    },
    methods: {
        // 获取章节树
        getChapterTree() {
            courseRelated.getCourseRelatedInfo(1).then(response => {
                const courseInfo = response.data
                if (courseInfo == null) return
                courseRelated.treeChapter({ ccParentId: courseInfo.courseId }).then(response => {
                    const { data } = response
                    this.chapterOptions = [{
                        id: courseInfo.courseId,
                        label: courseInfo.courseName,
                        children: data
                    }]
                    this.defaultShowNodes = [courseInfo.courseId]
                })
            })
        },
        // 节点点击事件
        handleNodeClick(data, node) {
            this.currentNode = data;
            courseRelated.getCourseResource({ chapterId: data.id, resourceCategory: 'lab' }).then(response => {
                this.videoList = (response.rows || []).filter(item => item.crFileUrl);
            })
        },
        playVideo(item) {
        this.currentVideo = {
                ...item,
                videoUrl: this.baseURL + item.crFileUrl
        };
        this.dialogVisible = true;
      },
        downloadFile(item) {
            const link = document.createElement('a');
            link.href = this.baseURL + item.crFileUrl;
            link.download = item.crName || '下载文件';
            link.style.display = 'none';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        },
      handleClose() {
        if (this.$refs.videoPlayer) {
          this.$refs.videoPlayer.pause();
        }
        this.dialogVisible = false;
        this.currentVideo = null;
        },
        selectItem(index) {
            // 兼容旧逻辑，实际已不用
        },
        checkTextOverflow() {},
        getPoster(video) {
            // 优先用后端返回的poster字段，否则用视频首帧，否则用默认图片
            if (video.poster) return this.baseURL + video.poster;
            if (video.crFileUrl) return this.baseURL + video.crFileUrl + '?x-oss-process=video/snapshot,t_1000,f_jpg,w_300';
        },
        removeFileSuffix(name) {
            if (!name) return '';
            return name.replace(/\.[^\.]+$/, '');
        },
        filterTree(data, keyword) {
            return data
                .map(node => {
                    const children = node.children ? this.filterTree(node.children, keyword) : [];
                    if (node.label.includes(keyword) || children.length) {
                        return {
                            ...node,
                            children
                        };
                    }
                    return null;
                })
                .filter(Boolean);
        },
    },
    watch: {
        nodeName(val) {
            if (!val) {
                this.getChapterTree(); // 还原全部
            } else {
                const root = this.chapterOptions[0];
                if (root && root.children) {
                    const filtered = this.filterTree(root.children, val);
                    this.chapterOptions = [{
                        ...root,
                        children: filtered
                    }];
                }
            }
        }
    },
}
</script>

<style lang="scss" scoped>
.program-resources {
    display: flex;
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

.sidebar {
    width: 20%;
    height: 100%;
    box-sizing: border-box;
    background: rgb(68,206,246);
    background: linear-gradient(135deg, rgba(68,206,246,1) 0%, rgba(94,129,255,1) 40%);
    color: white;
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    border-top-left-radius: 20px;
    overflow-y: auto;

    .logo {
        padding: 20px 30px;
        h2 {
            margin: 0;
            font-size: 24px;
            font-weight: bold;
        }
    }

    .nav-list {
        margin-top: 20px;
        
        .head-container {
            padding: 0 20px;
        }
        
        .custom-tree {
            background: transparent;
            color: white;
            
            :deep(.el-tree-node__content) {
                height: 50px;
                padding: 0 30px;
            margin: 5px 20px;
            margin-right: 0px;
            border-radius: 20px 0px 0px 20px;
                transition: all 0.3s ease;
            
            &:hover {
                background: rgba(255, 255, 255, 0.1);
                }
            }
            
            :deep(.el-tree-node.is-current > .el-tree-node__content) {
                background: white;
                color: #5e81ff;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            }
            
            :deep(.el-tree-node__label) {
                font-size: 16px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        }
    }
}

.content {
    width: 80%;
    padding: 20px;
    box-sizing: border-box;
    padding-right: 0;
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    .content_top_title {
        font-size: 24px;
        font-weight: bold;
        margin-bottom: 20px;
        color: #333;
        padding-left: 20px;
    }
    .content_video_box {
        width: 100%;
        height: calc(100% - 75px);
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
        gap: 25px;
        overflow-y: auto;
        padding: 20px;
        box-sizing: border-box;
        .content_video_box_item {
            width: 100%;
            height: 300px;
            background: #ffffff;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            cursor: pointer;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            &:hover {
                transform: translateY(-5px);
                box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
                .play-button {
                    opacity: 1;
                }
            }
            .video-thumbnail {
                position: relative;
                width: 100%;
                height: 200px;
                overflow: hidden;
                padding: 10px;
                box-sizing: border-box;
                .video-player {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;
                    pointer-events: none;
                    border-radius: 10px;
                    background: #000;
                }
                .video-duration {
                    position: absolute;
                    bottom: 20px;
                    right: 20px;
                    background: rgba(0, 0, 0, 0.7);
                    color: white;
                    padding: 4px 8px;
                    border-radius: 4px;
                    font-size: 12px;
                }
                .play-button {
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                    width: 60px;
                    height: 60px;
                    background: rgba(255, 255, 255, 0.95);
                    border-radius: 50%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    opacity: 0;
                    transition: all 0.3s ease;
                    i {
                        font-size: 28px;
                        color: #5e81ff;
                    }
                }
            }
            .video-info {
                padding: 15px 20px;
                flex-grow: 1;
                .video-title {
                    font-size: 16px;
                    font-weight: bold;
                    color: #333;
                    margin-bottom: 10px;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
                .video-desc {
                    font-size: 14px;
                    color: #666;
                    line-height: 1.5;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
            }
            .buttom_box {
                width: 100%;
                padding: 10px 20px 20px;
                box-sizing: border-box;
                display: flex;
                align-items: center;
                justify-content: space-between;
            }
        }
    }
    .el-empty {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        min-height: 60vh;
    }
}
.Btn {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 35px;
  height: 35px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition-duration: 0.3s;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.199);
}

/* plus sign */
.sign {
  width: 100%;
  transition-duration: 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sign i {
  color: #fff;
  .el-icon-video-play{
    width: 20px;
    height: 20px;
  }
  .el-icon-download{
    width: 20px;
    height: 20px;
  }
}

/* text */
.text {
  position: absolute;
  right: 0%;
  width: 0%;
  opacity: 0;
  color:#fff;
  font-size:14px;
  font-weight: 600;
  transition-duration: 0.1s;
}
/* hover effect on button width */
.Btn:hover {
  width: 75px;
  border-radius: 40px;
  transition-duration: 0.3s;
}

.Btn:hover .sign {
  width: 30%;
  transition-duration: 0.3s;
  padding:0 5px;
}
/* hover effect button's text */
.Btn:hover .text {
  opacity: 1;
  width: 70%;
  transition-duration: 0.1s;
  padding-left: 15px;
  box-sizing: border-box;
}
/* button click effect*/
.Btn:active {
  transform: translate(2px, 2px);
}
.bgcolor1{
    background-color: #89c3eb;
}
.bgcolor2{
    background-color: #b0a4e3;
}
  // 弹窗样式
  .video-dialog {
    .video-player {
      width: 100%;
      height: auto;
      max-height: 70vh;
      object-fit: contain;
    }
  
    .el-dialog__body {
      padding: 0;
      background: #000;
    }
  
    .el-dialog__footer {
      text-align: center;
      background: #f5f5f5;
    }
  }
  
  ::v-deep .el-dialog {
    margin-top: 5vh !important;
    margin-left: 21% !important;
  }
  .el-icon-s-opportunity{
    color: #fff143;
  }
  ::v-deep .el-dialog__header{
    background: #262626;
  }
  ::v-deep .el-dialog__body{
    background: #262626;
  }
  ::v-deep .el-dialog__footer{
    background: #262626;
  }
  .video-player.no-controls::-webkit-media-controls { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-enclosure { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-panel { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-overlay-play-button { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-start-playback-button { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-play-button { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-timeline { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-current-time-display { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-time-remaining-display { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-mute-button { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-volume-slider { display: none !important; }
  .video-player.no-controls::-webkit-media-controls-fullscreen-button { display: none !important; }
</style>